import { ensureLF } from "."

export class CodeBuilder {
  readonly indent: number = 2
  codeBlocks: string[] = []

  public add(block?: string) {
    if (block === undefined) {
      return
    }
    this.codeBlocks.push(block)
  }

  public _addWithIndent(pre: string, main: string, post: string) {
    this.codeBlocks.push(`\
${pre}
  ${main.split('\n').join('\n  ')}
${post}
`)
  }

  public addFunction(name: string, param: string[], main: string) {
    this._addWithIndent(`def ${name}(${param.join(', ')}):`, main, '')
  }

  public build(): string {
    let result = ''

    for(let i=0; i< this.codeBlocks.length; i++) {
      const block = this.codeBlocks[i]
      result = ensureLF(result)
      result += `${block}`
      result = ensureLF(result)
    }

    return result
  }
}